package work.easy.monitor;


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Base64.Decoder;

/**
 *
 * @author YueXiao
 * @date 2017/11/15
 */
public class CryptoUtil {

    private final static Decoder base64Decoder = Base64.getDecoder();

    private CryptoUtil() {

    }

    public static byte[] encrypt(String algorithm, byte[] key, byte[] src) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        return cipher.doFinal(src);
    }

    public static byte[] encrypt(String algorithm, String base64Key, byte[] src) throws Exception {
        return encrypt(algorithm, Base64.getDecoder().decode(base64Key), src);
    }

    public static byte[] encrypt(String algorithm, String base64Key, String src) throws Exception {
        return encrypt(algorithm, Base64.getDecoder().decode(base64Key), src.getBytes());
    }

    public static byte[] encrypt(String algorithm, String base64Key, String src, String charset) throws Exception {
        return encrypt(algorithm, Base64.getDecoder().decode(base64Key), src.getBytes(charset));
    }

    public static byte[] decrypt(String algorithm, byte[] key, byte[] src) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        return cipher.doFinal(src);
    }

    public static byte[] decrypt(String algorithm, String base64Key, String base64Src) throws Exception {
        return decrypt(algorithm, base64Decoder.decode(base64Key), base64Decoder.decode(base64Src));
    }

    public static String decryptAsString(String algorithm, String base64Key, String base64Src) throws Exception {
        String rs = new String(decrypt(algorithm, base64Decoder.decode(base64Key), base64Decoder.decode(base64Src)),"UTF-8");
        return rs;
    }


    public static void main(String[] args) throws Exception {
        aesTest();
    }

    private static void desTest() throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("DES");
        kgen.init(56);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        String abc = new String(Base64.getEncoder().encode(enCodeFormat));

        byte[] b = encrypt("DES", "顶顶顶".getBytes(), Base64.getDecoder().decode(abc));

        byte[] rs = decrypt("DES", enCodeFormat, b);

        System.out.println(new String(rs));
    }

    private static void aesTest() throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        String abc = new String(Base64.getEncoder().encode(enCodeFormat));

        byte[] b = encrypt("AES", "顶顶顶".getBytes(), Base64.getDecoder().decode(abc));
        byte[] rs = decrypt("AES", enCodeFormat, b);
        System.out.println(new String(rs));
    }

}
